<?php

/**
 * This is the model class for table "{{itemcast}}".
 *
 * The followings are the available columns in table '{{itemcast}}':
 * @property integer $uid
 * @property integer $parent_cid
 * @property integer $cid
 * @property string $freight_payer
 * @property integer $post_fee
 * @property integer $express_fee
 * @property integer $ems_fee
 * @property string $location_state
 * @property string $location_city
 */
class Itemcast extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Itemcast the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'taobao_itemcast';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, parent_cid, cid, post_fee, express_fee, ems_fee', 'numerical', 'integerOnly'=>true),
			array('freight_payer, location_state, location_city', 'length', 'max'=>10),
			array('parent_cid,cid', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('uid, parent_cid, cid, freight_payer, post_fee, express_fee, ems_fee, location_state, location_city', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'uid' => 'Uid',
			'parent_cid' => '所属大类',
			'cid' => '所属的叶子类',
			'freight_payer' => '运费承担方式',
			'post_fee' => '平邮费用',
			'express_fee' => '快递费用',
			'ems_fee' => 'Ems费用',
			'location_state' => '所在地省份',
			'location_city' => '所在地城市',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('uid',$this->uid);
		$criteria->compare('parent_cid',$this->parent_cid);
		$criteria->compare('cid',$this->cid);
		$criteria->compare('freight_payer',$this->freight_payer,true);
		$criteria->compare('post_fee',$this->post_fee);
		$criteria->compare('express_fee',$this->express_fee);
		$criteria->compare('ems_fee',$this->ems_fee);
		$criteria->compare('location_state',$this->location_state,true);
		$criteria->compare('location_city',$this->location_city,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	protected function afterFind()	{
		parent::afterFind();
		$this->post_fee = $this->post_fee / 100;
		$this->express_fee = $this->express_fee / 100;
		$this->ems_fee = $this->ems_fee / 100;

		return true;
	}
	protected function beforeSave()	{
		parent::beforeSave();
		$this->post_fee = $this->post_fee * 100;
		$this->express_fee = $this->express_fee * 100;
		$this->ems_fee = $this->ems_fee * 100;

		return true;
	}
	/*
	 * 获取分类
	 * 返回数组
	 * $parent_cid 分类ID .0 为根分类
	 */
	function _getcats($parent_cid = 0) {		
		$req = new ItemcatsGetRequest;
		$_cats = array();
		$req->setFields("cid,parent_cid,name,is_parent");
		$req->setParentCid($parent_cid);
		$resp = Yii::app()->top->execute($req);
		if($resp->item_cats){
			foreach($resp->item_cats->item_cat as $itme_cat) {
				$_cats[$itme_cat->cid] = $itme_cat->name;
			}
		}
		return $_cats;
	}
}